import {createSlice} from "@reduxjs/toolkit";
import {useGetAllMaterialListAPI, useGetOneMaterialById} from "/src/apis/materialAPI.jsx";


const materialStore=createSlice({
    name:'material',
    initialState:{
        materialList:[],
        material:{}
    },
    reducers:{
        getAllMaterialList(state,action){
            state.materialList=action.payload;
        },
        getOneMaterialById(state,action){
            state.material=action.payload;
        }
    }
})

const {getAllMaterialList,getOneMaterialById}=materialStore.actions;

const fetchGetAllMaterialListByPage=(current,pageSize)=>{
    return async (dispatch)=>{
        const res=await useGetAllMaterialListAPI( current,pageSize)
        dispatch(getAllMaterialList(res.data))
    }
}

const fetchGetOneMaterialById=(id)=>{
    return async (dispatch)=>{
        const res=await useGetOneMaterialById(id)
        dispatch(getOneMaterialById(res.data))
    }
}

const materialReducer=materialStore.reducer;

export { getAllMaterialList,fetchGetAllMaterialListByPage ,getOneMaterialById,fetchGetOneMaterialById }

export default materialReducer;